package com.swu.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.swu.po.SurgeryDictionary;
import com.swu.mapper.SurgeryDictionaryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SurgeryExcelListener extends AnalysisEventListener<SurgeryExcel> {

    private final SurgeryDictionaryMapper surgeryDictionaryMapper;

    public SurgeryExcelListener(SurgeryDictionaryMapper surgeryDictionaryMapper) {
        this.surgeryDictionaryMapper = surgeryDictionaryMapper;
    }

    @Override
    public void invoke(SurgeryExcel data, AnalysisContext context) {
        log.info("解析到一条手术数据：{}", data);
        
        // 转换为实体对象
        SurgeryDictionary surgery = new SurgeryDictionary();
        surgery.setCode(data.getCode());
        surgery.setName(data.getName());
        surgery.setPinyinCode(data.getPinyinCode());
        surgery.setWubiCode(data.getWubiCode());
        surgery.setLength(data.getName().length());
        
        // 插入数据库
        surgeryDictionaryMapper.insert(surgery);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("手术数据解析完成！");
    }
} 